declare var Snap: any, mina: any;
import { Component, OnInit, DoCheck, Input, OnChanges, SimpleChanges, ViewChild, ElementRef } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { workaddService } from './workadd.Service';
// import { forbiddenNameValidator } from '../shared/forbidden-name.directive';
// import LCalendar from "../../assets/js/LCalendar.js";
import LCalendar from "../../assets/js/lCalendar (2).js";
// import LCalendar from  "../../assets/js/newLCalendar.js";
import { MyDatePickerModule, IMyDpOptions } from 'mydatepicker';
import { ConmonService } from "../../service/common.service";
// declare var $: any;
import * as $ from 'jquery';

export class workadd {
  // 报工日期
  WorkDate: string;
  WorkType: string;
  SubmitDate: any;
  // 项目名称
  ProName: string;
  //  项目编号
  ProId: string;
  // 工作内容
  WorkContent: string;
  // 消耗工时
  ConsumeTime: string;
}
@Component({
  selector: 'app-work-add',
  templateUrl: './work-add.component.html',
  styleUrls: ['./work-add.component.scss']
})

export class WorkAddComponent implements OnInit {
  title: string = "报工添加";
  changeLog: string[] = [];
  @Input() workadd: workadd = {
    WorkDate: '',
    SubmitDate: '',
    ProName: '',
    WorkType: '',
    ProId: '',
    WorkContent: '',
    ConsumeTime: ''
  };
  changeColor: boolean = false;
  // Initialized to specific date (09.10.2018). 
  private iswork: boolean = false;
  private isAddWork: boolean = false;
  private isleave: boolean = false;
  private isFishesd: boolean = false;
  private projectNameFlag: boolean = false;
  private projectIdFlag: boolean = false;
  private dataObj: any;
  private dataList: any;
  rejectReson: any;
  private calendar:any;
  private employId:string = '';
  private plpName:string = '';
  private proType:string = '';
  @ViewChild('submitBtn')
  submitBtn: ElementRef;
  ngDoCheck() {
    const ifdisabled = this.submitBtn.nativeElement.disabled;
    console.log(ifdisabled)
    // 禁用的状态
    if (!ifdisabled) {
      this.changeColor = true;
    } else {
      // 可用
      this.changeColor = false;
    };
    // 初始化确认按钮的颜色
    for (let i in this.workadd) {
      if (this.workadd[i] === '') {
        this.changeColor = false;
      }
    }
  };
  //  选择日期方法
  choiceDate() {
    var date = new Date();
    var time = date.getTime();
    console.log(time);
    var nowTime = new Date((time)-(60*60*24*1000*7));
    console.log(nowTime);
    var nowYear = nowTime.getFullYear();
    var nowMonth = nowTime.getMonth()+1;
    var nowDay = nowTime.getDate();
    var nowDate = nowYear+'-'+nowMonth+'-'+nowDay;
    console.log(nowDate);
    console.log(document.getElementsByTagName('body'));
    // let _this = this;
    if (document.getElementById('employeeDate')) {
      // 我的报工添加的选择报工日期日历插件调用
      var calendar = new LCalendar();
      calendar.init({
        'trigger': '#employeeDate', //标签id
        'type': 'date', //date 调出日期选择 datetime 调出日期时间选择 time 调出时间选择 ym 调出年月选择,
        'minDate': nowDate, //最小日期
        // 'minDate': '1990-01-01', //最小日期
        'maxDate': new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() //最大日期
        // 'maxDate': '2020-01-01' //最大日期
      });
    }
    
  }
  // 获取当前年月日
  getcurrentYYR() {
    let myDate = new Date();
    this.workadd.SubmitDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
    console.log(this.workadd.SubmitDate)
  };
  ngAfterViewInit() {
  };
  submitted = false;
  choiceWorkType($event) {
    var val = $event.target.value;
    if (val === '工作') {
      this.iswork = true;
      this.isleave = false;
      this.isAddWork = false;
      this.workadd.WorkType = '0';

    } else if (val === '加班') {
      this.isAddWork = true;
      this.isleave = false;
      this.iswork = false;
      this.workadd.WorkType = '1';
    } else if (val === '请假') {
      this.isleave = true;
      this.isAddWork = false;
      this.iswork = false;
      this.workadd.WorkType = '2';
    }
    // this.workadd.workType[this.workadd.workType.length-1];
  }
  // 发送请求，从后台传输数据
  results: string[];
  // 点击确定向后台发送请求
  ngSubmit($event) {
    let proNameValue = $("#projectName").val();
    let proIdValue = this.workadd.ProId;
    if (proIdValue.length === 6) {
      this.dataObj = {
        SearchProType: '0',
        ProName: proNameValue,
        ProId: proIdValue,
        ProMana: ''
      };
      let resData = this.ConmonService.withBodyData(this.dataObj, 'joinWorkProListInfo');
      console.log(resData);
      if (resData.body.ProInfo.length === 1) {
        var workAddBackData = this.ConmonService.withBodyData(this.workadd, 'AddDailyWork');
        if (workAddBackData.head.Result === 'S') {
          this.addSucess();
        } else {
          this.rejectReson = "添加失败：" + workAddBackData.head.RetMsg;
        };
      } else {
        this.rejectReson = "添加失败：项目编号与项目名称不匹配，请重新选择";
      };
    } else {
      this.rejectReson = "添加失败：项目编号必须为6位数字";
    }
    console.log(this.workadd)
  }
  //调项目名称的搜索，改变搜索结果的样式
  keyChangeColor(value: string, obj) {
    this.projectNameFlag = true;
    console.log(obj.target.nextElementSibling);
    this.dataObj = {
      SearchProType: '0',
      ProName: value,
      ProId: '',
      ProMana: ''
    };
    this.dataList = this.ConmonService.withBodyData(this.dataObj, 'joinWorkProListInfo');
    console.log(this.dataList);
    // let array = value.split(""); //分割
    // let  dsa=this.projectNameSearch.nativeElement;//获取全部商品
    setTimeout(function () {
      let dsa = $(obj.target.parentNode.nextElementSibling).find('li');
      for (let t = 0; t < dsa.length; t++) {
        let val = dsa.eq(t).text();
        console.log(val);
        let x = val.replace(value, "<span class='col_Color' style='color:#54a3ff;'>" + value + "</span>");
        $(obj.target.parentNode.nextElementSibling).find('li').eq(t).html(x);
      };
    }, 0)
  };
  //为项目名称搜索出来的结果赋值
  addProjectName(obj) {
    console.log(obj);
    this.workadd.ProName = obj.ProName;
    this.workadd.ProId = obj.ProId;
    this.proType = obj.DailyWorkType;
    this.projectNameFlag = false;
    // this.dataList = '';
  };
  //项目名称失去焦点
  pronameBlur() {
    let proNameValue = $("#projectName").val();
    // let proIdValue = $("#projectNumber").val();
    this.dataObj = {
      SearchProType: '0',
      ProName: proNameValue,
      ProId: '',
      ProMana: ''
    };
    let resData = this.ConmonService.withBodyData(this.dataObj, 'joinWorkProListInfo');
    console.log(resData);
    if (resData.body.ProInfo.length === 1) {
      this.workadd.ProId = resData.body.ProInfo[0].ProId;
      this.proType = resData.body.ProInfo[0].DailyWorkType;
    }
    let _this = this;
    setTimeout(function () {
      _this.projectNameFlag = false;
    }, 301)
    // this.dataList = '';
  };
  //项目编号搜索
  projectIdSearch(value: string, obj) {
    this.projectIdFlag = true;
    console.log(value);
    this.dataObj = {
      SearchProType: '0',
      ProName: '',
      ProId: value,
      ProMana: ''
    };
    this.dataList = this.ConmonService.withBodyData(this.dataObj, 'joinWorkProListInfo')
    console.log(this.dataList);
    // let array = value.split(""); //分割
    // let  dsa=this.projectNameSearch.nativeElement;//获取全部商品
    setTimeout(function () {
      // console.log($(obj.target.parentNode.nextElementSibling));
      let dsa = $(obj.target.parentNode.nextElementSibling).find('li');
      for (let t = 0; t < dsa.length; t++) {
        let val = dsa.eq(t).text();
        console.log(val);
        let x = val.replace(value, "<span class='col_Color' style='color:#54a3ff;'>" + value + "</span>");
        $(obj.target.parentNode.nextElementSibling).find('li').eq(t).html(x);
      };
    }, 0)
  };
  //项目编号失去焦点 
  projectNumberBlur() {
    // this.dataList = '';
    let _this = this;
    // let proNameValue = $("#projectName").val();
    let proIdValue = $("#projectNumber").val();
    this.dataObj = {
      SearchProType: '0',
      ProName: '',
      ProId: proIdValue,
      ProMana: ''
    };
    let resData = this.ConmonService.withBodyData(this.dataObj, 'joinWorkProListInfo');
    console.log(resData);
    if (resData.body.ProInfo.length === 1) {
      this.workadd.ProName = resData.body.ProInfo[0].ProName;
      this.proType = resData.body.ProInfo[0].DailyWorkType;
    }
    setTimeout(function () {
      _this.projectIdFlag = false;
    }, 301)
  };
  //为项目编号搜索出来的结果添加赋值
  addProjectId(obj) {
    this.workadd.ProName = obj.ProName;
    this.workadd.ProId = obj.ProId;
    this.proType = obj.DailyWorkType;
    // this.dataList = '';
    this.projectIdFlag = false;
  };
  //点击搜索图表调出所有相关数据
  iconSearch (obj,numFlag) {
    if (numFlag === 1) {
      this.projectNameFlag = false;
      this.projectIdFlag = true;
    } else {
      this.projectIdFlag = false;
      this.projectNameFlag = true;
    };
    // console.log($(obj.target.offsetParent).find('.border-bottom'));
    this.dataObj = {
      SearchProType: '0',
      ProName: '',
      ProId: '',
      ProMana: ''
    };
    this.dataList = this.ConmonService.withBodyData(this.dataObj, 'joinWorkProListInfo')
    console.log(this.dataList);
    // let array = value.split(""); //分割
    // let  dsa=this.projectNameSearch.nativeElement;//获取全部商品
    // setTimeout(function () {
    //   let dsa = $(obj.target.nextElementSibling).find('li');
    //   for (let t = 0; t < dsa.length; t++) {
    //     let val = dsa.eq(t).text();
    //     console.log(val);
    //     let x = val.replace(value, "<span class='col_Color' style='color:#54a3ff;'>" + value + "</span>");
    //     $(obj.target.nextElementSibling).find('li').eq(t).html(x);
    //   };
    // }, 0)
  }
  reset () {
    this.projectNameFlag = false;
    this.projectIdFlag = false;
  }
  // 以下是表单验证部分  
  ngOnInit() {
    this.changeColor = false;
    // 初始化获取数据信息
    // this.getMembers();
    this.choiceDate();
    this.getcurrentYYR();
    
  }
  constructor(private router: Router,private activatedRoute: ActivatedRoute,private http: HttpClient,private ConmonService: ConmonService) {
    this.employId = ConmonService.employId;
    this.plpName = ConmonService.plpName;
  }
  // 报工添加点击确定判断是否跳转
  addSucess(): void {
    console.log(this.workadd);
    this.router.navigate(['/PersonTatal', true]);
  };
}

